<?php

namespace addons\workorder;

use think\Db;
use think\Addons;
use think\Request;
use app\common\library\Menu;
use app\common\library\Auth;
use addons\workorder\model\Engineer;

/**
 * 插件
 */
class Workorder extends Addons
{

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        // 创建菜单
        $menu = [
            [
                'name'    => 'workorder',
                'title'   => '工单管理',
                'icon'    => 'fa fa-th-list',
                'sublist' => [
                    [
                        'name'    => 'workorder/statistics',
                        'title'   => '数据统计',
                        'icon'    => 'fa fa-bar-chart fa-fw',
                        'weigh'   => '100',
                        'sublist' => [
                            ['name' => 'workorder/statistics/index', 'title' => '查看'],
                            ['name' => 'workorder/statistics/dateOrderTrend', 'title' => '筛选分析'],
                        ]
                    ],
                    [
                        'name'    => 'workorder/orders',
                        'title'   => '全部工单',
                        'icon'    => 'fa fa-list-ul',
                        'weigh'   => '99',
                        'sublist' => [
                            ['name' => 'workorder/orders/index', 'title' => '查看'],
                            ['name' => 'workorder/orders/add', 'title' => '增加'],
                            ['name' => 'workorder/orders/edit', 'title' => '编辑'],
                            ['name' => 'workorder/orders/del', 'title' => '删除'],
                            ['name' => 'workorder/orders/multi', 'title' => '批量更新'],
                            ['name' => 'workorder/orders/recyclebin', 'title' => '回收站'],
                            ['name' => 'workorder/orders/destroy', 'title' => '真实删除'],
                            ['name' => 'workorder/orders/restore', 'title' => '还原'],
                            ['name' => 'workorder/orders/records', 'title' => '查看沟通记录'],
                            ['name' => 'workorder/orders/import', 'title' => '导入'],
                        ]
                    ],
                    [
                        'name'    => 'workorder/engineers',
                        'title'   => '工程师管理',
                        'icon'    => 'fa fa-users',
                        'weigh'   => '98',
                        'sublist' => [
                            ['name' => 'workorder/engineers/index', 'title' => '查看'],
                            ['name' => 'workorder/engineers/add', 'title' => '增加'],
                            ['name' => 'workorder/engineers/edit', 'title' => '编辑'],
                            ['name' => 'workorder/engineers/del', 'title' => '删除'],
                            ['name' => 'workorder/engineers/multi', 'title' => '批量更新'],
                            ['name' => 'workorder/engineers/recyclebin', 'title' => '回收站'],
                            ['name' => 'workorder/engineers/destroy', 'title' => '真实删除'],
                            ['name' => 'workorder/engineers/restore', 'title' => '还原'],
                            ['name' => 'workorder/engineers/import', 'title' => '导入'],
                        ]
                    ],
                    [
                        'name'    => 'workorder/kbs',
                        'title'   => '知识库管理',
                        'icon'    => 'fa fa-graduation-cap',
                        'weigh'   => '97',
                        'sublist' => [
                            ['name' => 'workorder/kbs/index', 'title' => '查看'],
                            ['name' => 'workorder/kbs/add', 'title' => '增加'],
                            ['name' => 'workorder/kbs/edit', 'title' => '编辑'],
                            ['name' => 'workorder/kbs/del', 'title' => '删除'],
                            ['name' => 'workorder/kbs/multi', 'title' => '批量更新'],
                            ['name' => 'workorder/kbs/recyclebin', 'title' => '回收站'],
                            ['name' => 'workorder/kbs/destroy', 'title' => '真实删除'],
                            ['name' => 'workorder/kbs/restore', 'title' => '还原'],
                            ['name' => 'workorder/kbs/import', 'title' => '导入'],
                        ]
                    ],
                    [
                        'name'    => 'workorder/category',
                        'title'   => '工单类别管理',
                        'icon'    => 'fa fa-leaf fa-fw',
                        'weigh'   => '96',
                        'sublist' => [
                            ['name' => 'workorder/category/index', 'title' => '查看'],
                            ['name' => 'workorder/category/add', 'title' => '增加'],
                            ['name' => 'workorder/category/edit', 'title' => '编辑'],
                            ['name' => 'workorder/category/del', 'title' => '删除'],
                            ['name' => 'workorder/category/multi', 'title' => '批量更新'],
                            ['name' => 'workorder/category/recyclebin', 'title' => '回收站'],
                            ['name' => 'workorder/category/destroy', 'title' => '真实删除'],
                            ['name' => 'workorder/category/restore', 'title' => '还原'],
                            ['name' => 'workorder/category/import', 'title' => '导入'],
                        ]
                    ],
                    [
                        'name'    => 'workorder/fields',
                        'title'   => '表单字段管理',
                        'icon'    => 'fa fa-wpforms',
                        'weigh'   => '95',
                        'sublist' => [
                            ['name' => 'workorder/fields/index', 'title' => '查看'],
                            ['name' => 'workorder/fields/add', 'title' => '增加'],
                            ['name' => 'workorder/fields/edit', 'title' => '编辑'],
                            ['name' => 'workorder/fields/del', 'title' => '删除'],
                            ['name' => 'workorder/fields/multi', 'title' => '批量更新'],
                            ['name' => 'workorder/fields/import', 'title' => '导入'],
                        ]
                    ],
                    [
                        'name'    => 'workorder/urgentrank',
                        'title'   => '紧急程度管理',
                        'icon'    => 'fa fa-balance-scale',
                        'weigh'   => '94',
                        'sublist' => [
                            ['name' => 'workorder/urgentrank/index', 'title' => '查看'],
                            ['name' => 'workorder/urgentrank/add', 'title' => '增加'],
                            ['name' => 'workorder/urgentrank/edit', 'title' => '编辑'],
                            ['name' => 'workorder/urgentrank/del', 'title' => '删除'],
                            ['name' => 'workorder/urgentrank/multi', 'title' => '批量更新'],
                            ['name' => 'workorder/urgentrank/recyclebin', 'title' => '回收站'],
                            ['name' => 'workorder/urgentrank/destroy', 'title' => '真实删除'],
                            ['name' => 'workorder/urgentrank/restore', 'title' => '还原'],
                            ['name' => 'workorder/urgentrank/import', 'title' => '导入'],
                        ]
                    ],
                    [
                        'name'    => 'workorder/evaluate',
                        'title'   => '工单评价管理',
                        'icon'    => 'fa fa-star',
                        'weigh'   => '93',
                        'sublist' => [
                            ['name' => 'workorder/evaluate/index', 'title' => '查看'],
                            ['name' => 'workorder/evaluate/add', 'title' => '增加'],
                            ['name' => 'workorder/evaluate/edit', 'title' => '编辑'],
                            ['name' => 'workorder/evaluate/del', 'title' => '删除'],
                            ['name' => 'workorder/evaluate/multi', 'title' => '批量更新'],
                            ['name' => 'workorder/evaluate/import', 'title' => '导入'],
                        ]
                    ],
                    [
                        'name'    => 'workorder/submitchannel',
                        'title'   => '问题提交渠道管理',
                        'icon'    => 'fa fa-map',
                        'weigh'   => '0',
                        'sublist' => [
                            ['name' => 'workorder/submitchannel/index', 'title' => '查看'],
                            ['name' => 'workorder/submitchannel/add', 'title' => '增加'],
                            ['name' => 'workorder/submitchannel/edit', 'title' => '编辑'],
                            ['name' => 'workorder/submitchannel/del', 'title' => '删除'],
                            ['name' => 'workorder/submitchannel/multi', 'title' => '批量更新'],
                            ['name' => 'workorder/submitchannel/import', 'title' => '导入'],
                        ]
                    ]
                ]
            ]
        ];
        Menu::create($menu);
        return true;
    }

    /**
     * 插件更新方法
     * @return bool
     */
    public function upgrade()
    {
        // v1.0.1 增加数据统计
        if (!Db::name('auth_rule')->where('name', 'workorder/statistics')->value('id')) {
            $menu = [
                [
                    'name'    => 'workorder/statistics',
                    'title'   => '数据统计',
                    'icon'    => 'fa fa-bar-chart fa-fw',
                    'weigh'   => '100',
                    'sublist' => [
                        ['name' => 'workorder/statistics/index', 'title' => '查看']
                    ]
                ]
            ];
            Menu::create($menu, 'workorder');
        }
        // 兼容v1.0.0 如果存在分派过工程师的工单，但所有工程师均无工单量数据，则计算
        $isCalcOrderQuantity = Db::name('workorder_orders')
            ->where('engineer_id', '>', 0)
            ->where('deletetime', null)
            ->count('id');
        $orderQuantitySum    = Db::name('workorder_engineers')->where('deletetime', null)->sum('work_order_quantity');

        if ($isCalcOrderQuantity && $orderQuantitySum == 0) {
            $engineers = Engineer::all(['status' => '1']);
            foreach ($engineers as $engineer) {
                $engineer->work_order_quantity = Db::name('workorder_orders')
                    ->where('engineer_id', $engineer->id)
                    ->where('deletetime', null)
                    ->count('id');
                $engineer->save();
            }
        }
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        Menu::delete('workorder');
        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {
        $this->upgrade();
        Menu::enable('workorder');
        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {
        Menu::disable('workorder');
        return true;
    }

    /**
     * 会员中心边栏后
     * @return mixed
     * @throws \Exception
     */
    public function userSidenavAfter()
    {
        $isEngineer = false;
        $auth       = Auth::instance();
        if ($auth->isLogin() && Engineer::where('user_id', $auth->id)->where('status', '1')->value('id')) {
            $isEngineer = true;
        }
        $request        = Request::instance();
        $actionname     = strtolower($request->action());
        $controllername = strtolower($request->controller());
        $data           = [
            'actionname' => $controllername . '.' . $actionname,
            'isEngineer' => $isEngineer
        ];
        return $this->fetch('view/hook/user_sidenav_after', $data);
    }
}
